import { getFormFields } from '@/utils/formUtils'
import type { FormFieldConfig } from '@/types/form'
import { usePromotionCodeStore } from '@/store/promotionCodeStore'

// 表格列配置
export const tableColumns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '項目名稱',
    dataIndex: 'name',
  },
  {
    title: 'CODE',
    dataIndex: 'code',
  },
  {
    title: '分成方式',
    dataIndex: 'shareType',
  },
  {
    title: '員工分成',
    dataIndex: 'staffShare',
  },
  {
    title: '合作人分成',
    dataIndex: 'partnerShare',
  },
  {
    title: '最後修改人',
    dataIndex: 'updatedBy',
  },
  {
    title: '最後修改時間',
    dataIndex: 'updatedAt',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]

//表單
export const FormField: Record<string, FormFieldConfig> = {
  name: {
    label: '名稱',
    type: 'input', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入項目名稱',
    rules: [{ required: true, message: '請輸入項目名稱' }],
  },
  code: {
    label: 'CODE',
    type: 'select', //作用是將一個值斷言為字面量類型
    placeholder: '請選擇CODE',
    defaultValue: '',
    options: [],
  },
  shareType: {
    label: '分成方式',
    type: 'radio', //作用是將一個值斷言為字面量類型
    placeholder: '請選擇分成方式',
    defaultValue: 1,
    options: [
      { label: '固定金額', key: 1 },
      { label: '按比例', key: 2 },
    ],
  },
  staffShare: {
    label: '員工分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入員工分成',
    rules: [{ required: true, message: '請輸入員工分成', type: 'number' }],
    min:0,
    addonBefore: (formState: any) => (formState.shareType === 1 ? '$' : ''),
    addonAfter: (formState: any) => (formState.shareType === 2 ? '%' : ''),
  },
  partnerShare: {
    label: '合作人分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入合作人分成',
    rules: [{ required: true, message: '請輸入合作人分成', type: 'number' }],
    min:0,
    addonBefore: (formState: any) => (formState.shareType === 1 ? '$' : ''),
    addonAfter: (formState: any) => (formState.shareType === 2 ? '%' : ''),
  },
}

// 創建表單字段
export const getCreteFieldConfig = async (type: number, fields?: string[]) => {
  const formFields = await getCommonFieldConfig(type, fields)
  return formFields
}

export const getEditFieldConfig = async(type: number, fields?: string[]) => {
  const formFields = await getCommonFieldConfig(type, fields)
  return formFields
}

const getCommonFieldConfig=async (type: number, fields?: string[]) => {
  const formFields = getFormFields(FormField, fields)
  const promotionCodeStore = usePromotionCodeStore()
 const codeOptions = await promotionCodeStore.fetchPromotionCodeOptions(type)
 formFields.code.options = [
  {
    label: '冇code匹配（默認）',
    key: '',
  },
 ...codeOptions
]
  return formFields
}



